<template>
  <div v-loading.fullscreen.lock="loading">

  </div>
</template>

<script>
import { SendCodeApi } from '@/services/Public'
import { userPermissionApi } from "@/services/UserPermission";
import { ProjectListApi } from "@/services/ProjectCenter";
import { initGlobalState } from "qiankun";
export default {
  components: {

  },
  data () {
    return {
      code: '',
      userId: '',
      userMenu: [],
      loading: false,
      userInfo: {},
      // 项目列表请求参数
      requestParam: {
        province_no: "",
        city_no: "",
        area_no: "",
        company_id: "",
        project_name: "",
        page: 1,
        per_page: 10000000
      },
    };
  },
  watch: {

  },
  props: {
    menuIndex: {
      type: Number,
      default: 0
    },
    routeUrl: {
      type: String,
      default: ''
    }
  },
  computed: {

  },
  methods: {
    // 用code换token
    getToken () {
      return SendCodeApi.getToken({
        params: {
          code: this.code,
          isDebug: this.$route.query.isDebug || '0',
          userNo: this.$route.query.userNo || ''
        }
      }).then(res => {

        if (res.code === 0) {
          window.localStorage.clear()
          window.sessionStorage.clear()
          this.userInfo = res.data.user || {}
          window.localStorage.setItem('zlj', '1')
          window.localStorage.setItem('access_token', res.data.token)
          window.sessionStorage.setItem('access_token', res.data.token)
          console.log(res.data.user.user_id, '9999')
          window.localStorage.setItem('user_id', res.data.user.user_id)
        } else {
          this.$router.push({
            path: '/blank'
          })
        }
      })
    },
    // 获取用户详情
    getUserInfo () {
      return userPermissionApi.getUserInfo({
        data: {
          user_id: this.userInfo.user_id
        }
      }).then(res => {
        if (res.code === 0) {
          res.data.job_list = res.data.job_data
          window.localStorage.setItem("userInfo", JSON.stringify(res.data));
        }
      })
    },
    // 获取用户权限
    getUserAuth () {
      return userPermissionApi.getUserAuth({
        data: {
          user_id: this.userInfo.user_id
        }
      }).then(res => {
        let userAuthList = res.data.list;
        if (userAuthList) {
          this.$store.dispatch("setUserAuth", res.data.list);
        }
      },
        err => {
          console.log(err);
        }
      );
    },
    // 根据域名获取单位信息
    getCompanyByDomain () {
      let domain =
        location.host.indexOf("localhost") >= 0
          ? "dev_platform.ihibuilding.cn"
          : location.host;
      // let domain = 'dev_platform.ihibuilding.cn'
      return userPermissionApi.getCompanyByDomain({
        data: {
          company_domain: domain
        }
      }).then(res => {
        window.localStorage.setItem(
          "companyInfo",
          JSON.stringify(res.data)
        );
        this.companyInfo = res.data;
        this.containerStyle = {
          backgroundImage: `url(${this.companyInfo.image_picture})`
        }
        // 设置页面title为接口返回的数据
        let title = document.getElementsByTagName("title");
        title[0].innerText = this.companyInfo.company_name;
        // 如果设置了公司logo，就将浏览器标签页的icon修改为logo
        if (this.companyInfo.company_logo) {
          let logo = document.querySelector(".tab-icon");
          logo.href = this.companyInfo.company_logo;
        }
      }, err => {
        console.log(err);
      }
      );
    },
    // 获取项目列表
    getProjectList () {
      return ProjectListApi.getProjectList({
        params: this.requestParam
      }).then(res => {
        if (res.code === 0) {
          localStorage.setItem("AllProjectList", JSON.stringify(res.data.list));
          // 判断是否已选择了项目，已选择不处理，未选择默认取第一个
          let storage = window.localStorage.getItem("choosedProject") || "{}";
          let choosedProject = JSON.parse(storage);
          choosedProject = choosedProject.project_id ? choosedProject : res.data.list[0];
          this.$store.dispatch("setProject", choosedProject);
          this.$store.dispatch("setProjectIdList", [
            choosedProject.project_id
          ]);
          let state = {
            projectList: {
              list: []
            },
            choosedProject: {}
          };
          state.choosedProject = choosedProject.project_id ? choosedProject : res.data.list[0];
          state.projectList.list = res.data.list || [];
          const actions = initGlobalState(state);
          actions.setGlobalState(state);
        }
        this.$router.push({
          path: '/' + this.routeUrl
        });
        this.$store.dispatch('setShowTop', false)
        this.$store.dispatch('setShowLeft', false)
      },
        err => {
          console.log(err);
        }

      );

    }
  },
  async created () {

  },
  async mounted () {

    this.$store.dispatch('setShowTop', true)
    this.$store.dispatch('setShowLeft', true)
    this.$store.dispatch("resetActiveTabsList", []);
    window.localStorage.clear()
    window.sessionStorage.clear()
    console.log(111)
    window.localStorage.setItem('zlj', '1')
    this.code = this.$route.query.code
    this.loading = true
    await this.getToken()
    setTimeout(async () => {
      await this.getUserInfo()
      await this.getUserAuth()
      await this.getCompanyByDomain()
      await this.getProjectList()
    }, 100);

  },
};
</script>

<style scoped lang="scss">
</style>
